package com.umeox.watch.moto.chat.ui

import android.content.Intent
import android.content.res.ColorStateList
import android.os.AsyncTask
import android.os.Bundle
import android.os.Handler
import android.text.SpannableStringBuilder
import android.text.Spanned
import android.text.style.TextAppearanceSpan
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.umeox.moto.common.ui.BaseActivity
import com.umeox.moto.common.utils.FileUtils
import com.umeox.moto.watch.themes.util.ThemeUtils
import com.umeox.moto.watch.themes.widget.ActionBar
import com.umeox.watch.moto.chat.R
import com.umeox.watch.moto.chat.db.AppDatabase
import com.umeox.watch.moto.chat.db.ChatMessage

/**
 * Created by Rambo 2021/8/25
 * Describe:
 */
class ConfirmDeleteActivity : BaseActivity() {


    companion object {
        const val EXTRA_CHAT_MSG_LIST = "ChatMsgList"
        private const val DELETE_COMPLETE = 0x10
        private const val FINISH_ACTIVITY = 0x12
    }

    private lateinit var mMessageTv: TextView
    private lateinit var mConfirmAb: ActionBar
    private lateinit var mDoneIv: ImageView

    private var mPhotos: List<ChatMessage>? = null
    private lateinit var mHandler: Handler

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.act_confirm_delete)
        mPhotos = intent.getParcelableArrayListExtra(EXTRA_CHAT_MSG_LIST)!!

        initHandler()

        mMessageTv = findViewById(R.id.tv_desc)
        mConfirmAb = findViewById(R.id.abConfirm)
        mDoneIv = findViewById(R.id.ivDone)
        mConfirmAb.visibility = View.VISIBLE
        mDoneIv.visibility = View.GONE
        if (mPhotos != null && mPhotos!!.isNotEmpty()) {
            val color: Int = ThemeUtils.textColorTertiary(this, R.color.dark_secondary_text_color)
            val pixelSize = 18
            val playUserPhotoTip =
                java.lang.String.format(getString(R.string.delete_tip), mPhotos!!.size)
            val strCount = mPhotos!!.size.toString()
            val index = playUserPhotoTip.indexOf(strCount)
            val redColors = ColorStateList.valueOf(color)
            val textAppearanceSpan =
                TextAppearanceSpan("sans-serif-condensed", 0, pixelSize, redColors, null)
            val spanBuilder = SpannableStringBuilder(playUserPhotoTip)
            spanBuilder.setSpan(
                textAppearanceSpan,
                index,
                index + strCount.length,
                Spanned.SPAN_EXCLUSIVE_INCLUSIVE
            )
            mMessageTv.text = spanBuilder
        }
        mConfirmAb.setOnClickListener {
            AsyncTask.execute {
                AppDatabase.get().chatMessageDao().delete(mPhotos!!)
                for (photo in mPhotos!!) {
                    val filePath: String = photo.filePath!!
                    FileUtils.deleteFiles(filePath)
                }
                mHandler.sendEmptyMessage(DELETE_COMPLETE)
            }
        }
    }

    private fun initHandler() {
        mHandler = Handler { msg ->
            if (msg.what == DELETE_COMPLETE) {
                mMessageTv.setText(R.string.str_delete_completed)
                mConfirmAb.visibility = View.GONE
                mDoneIv.visibility = View.VISIBLE
                mHandler.sendEmptyMessageDelayed(FINISH_ACTIVITY, 1000)
            } else if (msg.what == FINISH_ACTIVITY) {
                this@ConfirmDeleteActivity.setResult(RESULT_OK, Intent())
                finish()
            }
            false
        }
    }

}